package lc.二分;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/15
 */
public class 剑指Offer53I_在排序数组中查找数字I {

    public int search(int[] nums, int target) {

        int i = 0;
        for (int num : nums) {
            if (num == target) {
                i++;
            }
        }
        return i;
    }



    public int search1(int[] nums, int target) {
        int l = 0,r = nums.length-1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] >= target)
                r = mid -1;
            else
                l = mid +1;
        }
        int statr = l;
        l = 0;
        r = nums.length-1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] <= target)
                l = mid +1;
            else

                r = mid -1;
        }

        return r - statr + 1;

    }
}
